package cn.tedu.cinema.scheduling.service.impl;

import cn.tedu.cinema.scheduling.mapper.SchedulingMapper;
import cn.tedu.cinema.scheduling.pojo.dto.SchedulingQuery;
import cn.tedu.cinema.scheduling.pojo.dto.SchedulingSaveParam;
import cn.tedu.cinema.scheduling.pojo.entity.Scheduling;
import cn.tedu.cinema.scheduling.pojo.vo.SchedulingVO;
import cn.tedu.cinema.scheduling.service.SchedulingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SchedulingServiceImpl implements SchedulingService {
    @Autowired
    private SchedulingMapper schedulingMapper;

    @Override
    public void save(SchedulingSaveParam schedulingSaveParam) {
        log.debug("排片参数:{}", schedulingSaveParam);
        Scheduling scheduling = new Scheduling();
        BeanUtils.copyProperties(schedulingSaveParam, scheduling);
        if (scheduling.getId()==null){
            log.debug("排片新增业务,参数{}", scheduling);
            scheduling.setCreateTime(new Date());
            scheduling.setCinemaId(schedulingSaveParam.getCinemaId());
            schedulingMapper.insert(scheduling);
        }else {
            log.debug("排片修改业务,参数{}", scheduling);
            scheduling.setUpdateTime(new Date());
            schedulingMapper.update(scheduling);
        }

    }

    @Override
    public List<SchedulingVO> selectScheduling(SchedulingQuery schedulingQuery) {
        log.debug("排片查询业务,参数{}", schedulingQuery);
        List<SchedulingVO> list = schedulingMapper.select(schedulingQuery);
        log.debug("查询结果:{}", list);
        return list;
    }

    @Override
    public void deleteScheduling(Integer id) {
        log.debug("排片删除业务,参数{}", id);
        schedulingMapper.deleteScheduling(id);
    }

    @Override
    public SchedulingVO selectById(Integer id) {
        log.debug("根据id查询排片业务,参数{}", id);
        SchedulingVO schedulingVO = schedulingMapper.selectById(id);
        return schedulingVO;
    }

    @Override
    public List<SchedulingVO> selectByMovieId(SchedulingQuery schedulingQuery) {
        log.debug("根据电影名称查询排片业务,参数{}", schedulingQuery);
        List<SchedulingVO> list = schedulingMapper.selectWeekCount(schedulingQuery);
        log.debug("查询结果:{}", list);
        List<SchedulingVO> CountVO = schedulingMapper.selectCount(schedulingQuery);
        for (SchedulingVO schedulingVO : list){
            for (SchedulingVO schedulingVO1 : CountVO){
                if (schedulingVO.getHallId().equals(schedulingVO1.getHallId())){
                    Double weekRate = (double) schedulingVO.getWeekCount() / schedulingVO1.getCount();
                    System.out.println("--------------------"+ weekRate);
                    schedulingVO.setWeekRate(weekRate*100+"%");
                }
            }
        }
        return list;
    }

    @Override
    public List<SchedulingVO> selectScheduling1(SchedulingQuery schedulingQuery) {
        log.debug("排片查询业务,参数{}", schedulingQuery);
        List<SchedulingVO> list = schedulingMapper.selectshijian(schedulingQuery);
        log.debug("查询结果:{}", list);
        return list;
    }
}
